var netcore = require('../../../utils/network.js')
var tripleDes = require('../../../utils/CKCrypto.js')
var json=require('../../../utils/json_helper.js')
function getVideoInfo(courseId,videoId,callBack){
  var params = new Map();
  params.set("id", tripleDes.enCode(videoId))
  params.set("courseId", tripleDes.enCode(courseId))
  netcore.requestPost("video/getVideoInfo.do", params, function (success) {
    success.cover = tripleDes.deCode(success.cover+"")
    success.price = tripleDes.deCode(success.price+"")
    success.subHead = tripleDes.deCode(success.subHead+"")
    success.title = tripleDes.deCode(success.title+"")
    success.videoUrl = tripleDes.deCode(success.videoUrl+"")
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      duration: 3000
    })
  })
}
function getVideoBaseInfo(videoId,callBack){
  var params = new Map();
  params.set("videoId", tripleDes.enCode(videoId))
  netcore.requestPost("video/getVideoCourseInfo.do", params, function (success) {
    // success.id = tripleDes.deCode(success.id + "")
    // success.cover = tripleDes.deCode(success.cover + "")
    // success.price = tripleDes.deCode(success.price + "")
    // success.title = tripleDes.deCode(success.title + "")
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      duration: 3000
    })
  })
}
function getVideoSubjectList(videoId,callBack){
  var params = new Map();
  params.set("videoId", tripleDes.enCode(videoId))
  netcore.requestPost("subject/getSubjectInfo.do", params, function (success) {
    success.forEach(function(currentValue,index){
      currentValue.id = tripleDes.deCode(currentValue.id)
      currentValue.text = tripleDes.deCode(currentValue.text)
      currentValue.image = tripleDes.deCode(currentValue.image+"")
      currentValue.type = tripleDes.deCode(currentValue.type)
      try{
        currentValue.option = json.stringToJson(tripleDes.deCode(currentValue.option))
        if (currentValue.type == 2) {//填空题
          var tmp = tripleDes.deCode(currentValue.answer);
          console.log("answer:" + tmp)
          currentValue.answer = json.stringToJson(tmp)
        } else {
          currentValue.answer = tripleDes.deCode(currentValue.answer)
        }
      }catch(e){
        console.log(e)
      }
      currentValue.diff = tripleDes.deCode(currentValue.diff)
      currentValue.analysis = tripleDes.deCode(currentValue.analysis)
    })
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      duration: 3000
    })
  })
}
function getVideoListAndRemove(courseNo, removeVideoId,callBack) {
  getVideoLists(courseNo, function (list) {
    var delPosition=null;
    list.forEach(function (currentValue, index) {
      currentValue.id = tripleDes.deCode(currentValue.id + "")
      if (currentValue.id == removeVideoId) {
        delPosition=index
      }else{
        currentValue.cover = tripleDes.deCode(currentValue.cover + "")
        currentValue.title = tripleDes.deCode(currentValue.title + "")
        currentValue.subHead = tripleDes.deCode(currentValue.subHead + "")
        currentValue.price = tripleDes.deCode(currentValue.price + "")
        currentValue.videoUrl = tripleDes.deCode(currentValue.videoUrl + "")
      }
    })
    list.splice(delPosition, 1)
    callBack(list)
  })
}
function getVideoList(courseNo, callBack) {
  getVideoLists(courseNo,function(list){
    list.forEach(function (currentValue, index) {
      currentValue.id = tripleDes.deCode(currentValue.id + "")
      currentValue.cover = tripleDes.deCode(currentValue.cover + "")
      currentValue.title = tripleDes.deCode(currentValue.title + "")
      currentValue.subHead = tripleDes.deCode(currentValue.subHead + "")
      currentValue.price = tripleDes.deCode(currentValue.price + "")
      currentValue.videoUrl = tripleDes.deCode(currentValue.videoUrl + "")
    })
    callBack(list)
  })
}
function getVideoLists(courseNo,callBack){
  var params = new Map();
  params.set("courseNo", tripleDes.enCode(courseNo))
  netcore.requestPost("course/getVideoLists.do", params, function (success) {
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      duration: 3000
    })
  })
}
/**
 * 预下单
 * * @param payChannel     支付渠道 1、1021 支付宝   2、1012 微信   3、1088 粉币
     * @param payType   支付类型： 1、1001 粉币充值 2、1002 VIP充值 3、1003 视频购买
     * @param goodsId       商品ID （payType为1003时必须上传）  DES加密 视频ID 课程ID
     * @param goodsType 支付类型 1.	3001 视频 2.	3002 课件
 */
function getOrder(payType, goodsId, goodsType,callBack){
  var params = new Map();
  params.set("payChannel","1012")
  params.set("payType", payType)
  params.set("goodsId", tripleDes.enCode(goodsId))
  params.set("goodsType", goodsType)
  params.set("openid", tripleDes.enCode(getApp().globalData.openId))
  params.set("lat", getApp().globalData.userLocationInfo.lat)
  params.set("lng", getApp().globalData.userLocationInfo.lng)
  params.set("origin","xcs")
  netcore.requestPost("wx/pay.do", params,function(success){
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      duration: 3000
    })
  })
}
//支付
function buyVideo(_timeStamp, _nonceStr, prepayId,_paySign,callBack){
  wx.requestPayment({
    timeStamp: _timeStamp,
    nonceStr: _nonceStr,
    package: prepayId,//Sign=WXPay
    signType: 'MD5',
    paySign: _paySign,
    success:function(res){
      console.log("微信支付:%o", res)
      callBack(0,res)
    },
    fail:function(error){
      console.log("微信支付:%o", error)
      callBack(-1, error)
    },
    complete: function (res){
      console.log("微信支付流程完成")
    }
  })
}
module.exports = {
  getVideoInfo: getVideoInfo,
  getVideoList: getVideoList,
  getVideoSubjectList: getVideoSubjectList,
  getVideoListAndRemove: getVideoListAndRemove,
  getVideoBaseInfo: getVideoBaseInfo,
  getOrder: getOrder,
  buyVideo: buyVideo
}